<?php 

class DB{
	static function connect(){
		mysql_connect(constant("host"),constant("usuario"),constant("senha"));
		mysql_selectdb(constant("db"));
	}
	
	static function salvar($dados,$tabela,$elementos=null){
		
		if($elementos) foreach ($elementos as $k=>$v) $dados[$k]=$dados[$k];
		
		unset($dados["id"]);
		foreach ($dados as $d=>$v) {
			if(is_array($v)){
				continue;
			}
			if(!is_numeric($d)){$string_values.="'$v',";$string_tab.="$d,";}
		}
			
		$string_tab=substr($string_tab,0,strlen($string_tab)-1);
		$string_values=substr($string_values,0,strlen($string_values)-1);
		
		mysql_query("INSERT INTO $tabela($string_tab) VALUES($string_values)");
		//die("INSERT INTO $tabela($string_tab) VALUES($string_values)");
		
		return mysql_insert_id();
	}
	
	static function atualizar($dados,$where=null,$tabela,$elementos=null){
		unset($elementos["id"]);
		//print_r($dados);exit;
		//if($elementos) foreach ($elementos as $k=>$v) $dados[$k]=$dados[$v[""]];
		
		unset($dados["id"]);
		foreach ($dados as $d=>$v){	if(!is_numeric($d)&&!is_array($v))$string_values.="$d='$v' ,";}
		
		$string_values=substr($string_values,0,strlen($string_values)-1);
		$where=($where)?" WHERE $where":'';
		
		mysql_query("UPDATE $tabela SET $string_values $where");
		//die("UPDATE $tabela SET $string_values $where");
	}
	
	static function lista($campos,$from,$where=null,$limite=null,$ordem=null){
		//return DB::tabela(mysql_query("SELECT ".DB::parseStringSql($campos)." FROM $from LIMIT $limite"),$campos);
		//$campos+=array("tb.id"=>"id");
		$arr=DB::parseStringSql($campos);
		$sql="SELECT ".$arr[0]." FROM $from tb ";
		
		//print_r($arr);
		foreach($campos as $ca){
			if(is_array($ca)){
				$sql.="LEFT JOIN {$ca[0]} on({$ca[1]}) ";
			}
		}
		($where)?$sql.=" WHERE $where ":null;
		$sql.=($ordem)?" ORDER BY $ordem":null;
		$sql.=($limite)?" LIMIT $limite":null;
		
		//echo($sql);
		return DB::tabela(mysql_query($sql),$campos);
	}
	
	static function query($campos,$from,$where=null){
		return DB::fetchArray(mysql_query("SELECT ".DB::parseStringSql($campos)." FROM $from"),$campos);
	}
	
	static function find($id,$tabela,$where=null){
		//("SELECT * FROM $tabela $where");
		$where=($where)?"WHERE $where":"WHERE id=$id";
		//die("SELECT * FROM $tabela $where");
		return mysql_fetch_array(mysql_query("SELECT * FROM $tabela $where"));
	}
	
	static function fetchTodos($campos,$tabela,$where=null,$limit=null,$ordem=null,$group=null){
		$arr=array();
		
		$sql="SELECT $campos FROM $tabela ";
		($where)?$sql.=" WHERE $where ":null;
		($limit)?$sql.=" LIMIT 0, $limit ":null;
		($ordem)?$sql.=" ORDER BY $ordem ":null;
		($group)?$sql.=" GROUP BY $group ":null;
//die($sql);
		$query=mysql_query($sql);
		
		while ($rs=mysql_fetch_array($query)){
			foreach ($rs as $key=>$v) $a[$key]=$v;
			$arr[]=$a;
			unset($a);
		}
		
		if($arr) return $arr; else return false;
	}
	
	static function fetchTodosAssoc($campos,$tabela,$where=null,$groupby=null){
		$arr=array();
		
		$sql="SELECT $campos FROM $tabela ";
		($where)?$sql.=" WHERE $where ":null;
		($limit)?$sql.=" LIMIT 0, $limit ":null;
		//($ordem)?$sql.=" ORDER BY $ordem ":null;
		($ordem)?$sql.=" ORDER BY $ordem ":null;
		($groupby)?$sql.=" GROUP BY $groupby ":null;
		//die($sql);
		$query=mysql_query($sql);
		
		while ($rs=mysql_fetch_array($query, MYSQL_ASSOC)){
			foreach ($rs as $key=>$v) $a[$key]=$v;
			$arr[]=$a;
			unset($a);
		}
		//die(print_r($sql));
		if($arr) return $arr; else return false;
	}
	
	static function apagar($where,$tabela){
		if($where&&$tabela)mysql_query("DELETE FROM $tabela WHERE $where");
	}
	
	static function parseStringSql($campos){
		//$sql_campos="tb.id,";
		foreach ($campos as $id=>$c){
			if(is_array($c)){
				$arraux=explode(".",$c[2]);
				if($arraux[1]){
					//$sql_campos.="{$arraux[1]},";
				}else{
				}
					$sql_campos.="{$c[2]},";
				//$arraux
				//echo $sql_campos;
				$db=$c;
				continue;
			}
			$sql_campos.="$id,";
		}
		if($db){
			$sql_campos.="{$db[2]},";
		}
		//die($sql_campos);
		return array(substr($sql_campos,0 , strlen($sql_campos)-1),$db);		
	}
	
	static function fetchArray($query){
		while($rs=mysql_fetch_array($query)){
			$a[$c]=$rs[$id];
			$arr[]=$a;
		}
		
		return $arr;		
	}
	
	static function parseSql($sql){
		$arr=array();
		$query=mysql_query($sql);
		
		while ($rs=mysql_fetch_array($query, MYSQL_ASSOC)){
			foreach ($rs as $key=>$v) $a[$key]=$v;
			$arr[]=$a;
			unset($a);
		}
		//die(print_r($sql));
		if($arr) return $arr; else return false;
	}
	
	static function tabela($query,$campos){
		//die(print_r($campos));
		while($rs=mysql_fetch_array($query)){
			foreach ($campos as $id=>$c){
				$alias=($c[3])?$c[3]:null;
				
				if(is_array($c)){
					$aid=$c[3];
					if(!$aid){
						$aid=$c[2];
					}
					
					$aaid=explode(".",$aid);
					
					if($aaid[1]){
						$aid=$aaid[1];
						//echo $aaid[1];
					}
					
					if(!$alias){
						$alias=$aaid[1];
					}
					
					$a[$aid]=$rs[$alias];
				}else{
					$aid=$id;
					$pre=explode(".",$aid);
					
					if($pre[1]){
						$aid=$pre[1];
					}
						$a[$aid]=$rs[$aid];
				}
			}
			$arr[]=$a;
		}
		return $arr;
	}
}

?>
